package com.app.worldcupapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.List;

public class TeamListActivity extends AppCompatActivity{
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_list);
        initView();
        getData();
    }

    private void initView() {
        listView = findViewById(R.id.lv_listview);
    }
    private void getData() {

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

        String url = "http://10.1.62.14:8080/app18/teams/list ";

        JSONObject jsonObject = new JSONObject();



        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {

            @Override

            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                MyBean1 myBean1 = gson.fromJson(jsonObject.toString(), MyBean1.class);
                if (myBean1.isSuccess()) {
                    listView.setAdapter(new MyAdapter(myBean1.getList(),getBaseContext()));
                }else {
                    Toast.makeText(TeamListActivity.this, "error", Toast.LENGTH_SHORT).show();
                }

            }

        }, new Response.ErrorListener() {

            @Override

            public void onErrorResponse(VolleyError volleyError) {

                Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();

            }

        });

        requestQueue.add(jsonObjectRequest);

    }


}